#!/bin/bash

export TEXTDOMAIN=menumanager
[ ! -d /tmp/menumanager ] && mkdir /tmp/menumanager

build_list() {
	echo -n > /tmp/menumanager/list
	case $1 in
		Desktop)
			LC_ALL=C grep -liE "Categories=X-Desktop|Categories=desktop|Categories=screensaver|Categories=Accessibility|Categories=clock" /usr/share/applications/* > /tmp/menumanager/list_all;;
		System)
			LC_ALL=C grep -liE "Categories=X-System|Categories=System|Categories=HardwareSettings|Categories=Core|Categories=monitor|Categories=security" /usr/share/applications/* > /tmp/menumanager/list_all;;
		Setup)
			LC_ALL=C grep -liE "Categories=X-Setup|Categories=PackageManager" /usr/share/applications/* > /tmp/menumanager/list_all;;
		Utility)
			LC_ALL=C grep -liE "Categories=X-Utility|Categories=Utility|Categories=Electronics|Categories=Emulator|Categories=Engineering|Categories=Development|Categories=Building|Categories=Debugger|Categories=IDE|Categories=Profiling|Categories=ProjectManagement|Categories=RevisionControl|Categories=Translation|Categories=GUIDesigner|Categories=Archiving|Categories=Compression|Categories=TerminalEmulator|Categories=Shell|Categories=Documentation" /usr/share/applications/* > /tmp/menumanager/list_all;;
		Filesystem)
			LC_ALL=C grep -liE "Categories=X-Filesystem|Categories=File" /usr/share/applications/* > /tmp/menumanager/list_all;;
		Graphic)
			LC_ALL=C grep -liE "Categories=X-Graphic|Categories=2DGraphics|Categories=3DGraphics|Categories=raster|Categories=vector|Categories=Chart|Categories=FlowChart|Categories=Presentation|Categories=viewer|Categories=Photography|Categories=scanning|Categories=ocr" /usr/share/applications/* > /tmp/menumanager/list_all;;
		Document)
			LC_ALL=C grep -liE "Categories=X-Document|Categories=Publishing|Categories=WordProcessor|Categories=WebDevelopment|Categories=Dictionary|Categories=text|Categories=viewer" /usr/share/applications/* > /tmp/menumanager/list_all;;
		Business)
			LC_ALL=C grep -liE "Categories=X-Business|Categories=X-Calculate|Categories=Spread|Categories=ProjectManagement|Categories=Finance|Categories=Calculator" /usr/share/applications/* > /tmp/menumanager/list_all;;
		Personal)
			LC_ALL=C grep -liE "Categories=X-Personal|Categories=Contact|Categories=Calendar|Categories=pda|Categories=Languages|Categories=Science|Categories=ArtificialIntelligence|Categories=Astronomy|Categories=Biology|Categories=Chemistry|Categories=ComputerScience|Categories=DataVisualization|Categories=Economy|Categories=Electricity|Categories=Geography|Categories=Geology|Categories=Geoscience|Categories=History|Categories=ImageProcessing|Categories=Literature|Categories=Math|Categories=NumericalAnalysis|Categories=MedicalSoftware|Categories=Physics|Categories=Robotics|Categories=Sports|Categories=ParallelComputing" /usr/share/applications/* > /tmp/menumanager/list_all;;
		Network)
			LC_ALL=C grep -liE "Categories=X-Network|Categories=Dialup|Categories=HamRadio|Categories=RemoteAccess" /usr/share/applications/* > /tmp/menumanager/list_all;;
		Internet)
			LC_ALL=C grep -liE "Categories=X-Internet|Categories=WebBrowser|Categories=Chat|Categories=InstantMessaging|Categories=VideoConference|Categories=Email|Categories=News|Categories=Telephony|Categories=IRCClient|Categories=FileTransfer|Categories=P2P" /usr/share/applications/* > /tmp/menumanager/list_all;;
		Multimedia)
			LC_ALL=C grep -liE "Categories=X-Multimedia|Categories=audio|Categories=player|Categories=recorder|Categories=video|Categories=tv|Categories=music|Categories=midi|Categories=mixer|Categories=sequencer|Categories=diskburning" /usr/share/applications/* > /tmp/menumanager/list_all;;
		Fun)
			LC_ALL=C grep -liE "Categories=X-Fun|Game|Categories=Amusement|Categories=Simulation|Categories=RolePlaying" /usr/share/applications/* > /tmp/menumanager/list_all;;
	esac
	
	while read I; do
		IFS='=' read C1 NAME <<< "`LC_ALL=C grep -F 'GenericName=' "$I"`"
		IFS='=' read C1 DESC <<< "`LC_ALL=C grep "^Name=" "$I"`"
		[ ! "$NAME" ] && NAME="$DESC"
		[ "`LC_ALL=C grep -F 'NoDisplay=true' "$I"`" ] && ICON='gtk-no' || ICON='gtk-yes'
		echo "${NAME}|${DESC}|${ICON}|$I" >> /tmp/menumanager/list
	done < /tmp/menumanager/list_all
}

switch_status(){
	APP="$1"
	if grep "^NoDisplay=true" ${APP}; then
		sed -i "s%NoDisplay=true%NoDisplay=false%" ${APP}
	else
		if grep "^NoDisplay=false" ${APP}; then
			sed -i "s%NoDisplay=false%NoDisplay=true%" ${APP}
		else
			echo "NoDisplay=true" >> ${APP}
		fi
	fi
}

export -f build_list switch_status



build_list Desktop

export menuManager='
<window title="'$(gettext "Menu Manager")'" icon-name="gtk-properties" default-width="550">
<vbox space-fill="true" space-expand="true">
  '"`/usr/lib/gtkdialog/xml_info fixed menu.svg 60 "$(gettext "This controls what apps are shown in the menu on your system. Click on app to hide/show.")"`"'
  <vbox space-fill="true" space-expand="true">
    <frame '$(gettext "Menu items")'>
      <hbox height-request="350" space-fill="true" space-expand="true">
        <hbox space-expand="false" space-fill="false">
          <tree name="category" selected-row="0" exported_column="1" column-visible="true|false" space-expand="false" space-fill="false">
            <label>'$(gettext 'Category')'|command</label>
            <variable>CATEGORY</variable>
            <item stock="gtk-Desktop">'$(gettext 'Desktop')'|Desktop</item>
            <item stock="gtk-System">'$(gettext 'System')'|System</item>
            <item stock="gtk-Setup">'$(gettext 'Setup')'|Setup</item>
            <item stock="gtk-Utility">'$(gettext 'Utility')'|Utility</item>
            <item stock="gtk-Filesystem">'$(gettext 'Filesystem')'|Filesystem</item>
            <item stock="gtk-Graphic">'$(gettext 'Graphic')'|Graphic</item>
            <item stock="gtk-Document">'$(gettext 'Document')'|Document</item>
            <item stock="gtk-Business">'$(gettext 'Business')'|Business</item>
            <item stock="gtk-Personal">'$(gettext 'Personal')'|Personal</item>
            <item stock="gtk-Network">'$(gettext 'Network')'|Network</item>
            <item stock="gtk-Internet">'$(gettext 'Internet')'|Internet</item>
            <item stock="gtk-Multimedia">'$(gettext 'Multimedia')'|Multimedia</item>
            <item stock="gtk-Fun">'$(gettext 'Fun')'|Fun</item>
            <width>140</width><height>112</height>
            <action signal="changed">build_list $CATEGORY</action>
            <action signal="changed">refresh:LIST</action>
          </tree>
        </hbox>
        <hbox space-fill="true" space-expand="true">
          <tree hover-selection="true" exported_column="2" column-visible="true|true|false|false">
            <variable>LIST</variable>
            <label>'$(gettext "App")'|'$(gettext "Description")'|icon|file</label>
            <input file stock-column="2">/tmp/menumanager/list</input>
            <action signal="button-release-event">switch_status "$LIST"</action>
            <action signal="button-release-event">build_list $CATEGORY</action>
            <action signal="button-release-event">refresh:LIST</action>
          </tree>
        </hbox>
      </hbox>
    </frame>
  </vbox>
 <hbox space-expand="false" space-fill="false">
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Cancel")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
      <action>exit:Cancel</action>
    </button>
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Ok")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
      <action>exit:OK</action>
    </button>
    '"`/usr/lib/gtkdialog/xml_scalegrip`"'
  </hbox>
 </vbox>
</window>'

ln -s /usr/local/lib/X11/pixmaps/*48.png /tmp/menumanager 2>/dev/null
echo '
style "icon-style" {
	GtkStatusbar::shadow_type = GTK_SHADOW_NONE

	stock["gtk-Desktop"]    = {{ "x48.png", *, *, *}}
	stock["gtk-System"]	    = {{ "pc48.png", *, *, *}}
	stock["gtk-Setup"]      = {{ "configuration48.png", *, *, *}}
	stock["gtk-Utility"]    = {{ "utility48.png", *, *, *}}
	stock["gtk-Filesystem"] = {{ "folder48.png", *, *, *}}
	stock["gtk-Graphic"]    = {{ "paint48.png", *, *, *}}
	stock["gtk-Document"]   = {{ "word48.png", *, *, *}}
	stock["gtk-Business"]   = {{ "spread48.png", *, *, *}}
	stock["gtk-Personal"]   = {{ "date48.png", *, *, *}}
	stock["gtk-Network"]    = {{ "connect48.png", *, *, *}}
	stock["gtk-Internet"]   = {{ "www48.png", *, *, *}}
	stock["gtk-Multimedia"] = {{ "multimedia48.png", *, *, *}}
	stock["gtk-Fun"]        = {{ "games48.png", *, *, *}}
	stock["gtk-BB"]         = {{ "pet48.png", *, *, *}}
	}
class "GtkWidget" style "icon-style"' > /tmp/menumanager/gtkrc

export GTK2_RC_FILES=/root/.gtkrc-2.0:/tmp/menumanager/gtkrc
. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
eval $(gtkdialog -p menuManager)

case $EXIT in
OK)
	# fix menu
	/usr/lib/gtkdialog/box_splash -close never -text "$(gettext 'Please wait. Updating menu...')" &
	pid=$!
	fixmenus
	ps -A | grep -q "jwm" && (jwm -reload || jwm -restart)
	kill -9 $pid
	sleep 1;;
esac
